﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmRMBack : Form
    {
        public FrmRMBack()
        {
            InitializeComponent();
        }

        DataTable DtIn;
        string txtBillNos;
        string txtRowNo;
       
        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {

                try
                {
                    //查询出入库数量
                    DtIn = Bll_RMOut.getDetailByUniqueCode(txtBoxCode.Text, txtBillNos);
                    if (DtIn != null && DtIn.Rows.Count > 0)
                    {
                        //if(DtIn.Rows[0]["Status"].ToString()=="0")
                        //{
                        //Bll_RMOut.UpdateRMDetailByUniqueCode(txtBoxCode.Text,txtBillNos);
                        //}

                        var recomLocationSN = DtIn.Rows[0]["Location_SN"].ToString();
                        try
                        {
                            lblSN.Text = recomLocationSN.Split('#')[1] + recomLocationSN.Split('#')[2];
                        }
                        catch (Exception ex)
                        {
                            lblSN.Text = DtIn.Rows[0]["Storage_SN"].ToString();
                        }
                        
                        txtSN.Focus();
                    }
                    else
                    {
                        txtBoxCode.Text = "";
                        MessageBox.Show("请选择正确的条码！");
                        return;
                    }
                }
                catch 
                {
                    txtBoxCode.Text = "";
                    MessageBox.Show("数据有错误!");
                }
            }
        }

        private void FrmRMIn_Load(object sender, EventArgs e)
        {
            txtBoxCode.Focus();
        }

        private void txtSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    var boolToERP = true;
                    if (DtIn.Rows[0]["Status"].ToString() == "0")
                    {
                        //都传ERP
                        //boolToERP = false;
                    }
                    var billno = DtIn.Rows[0]["BillNo"].ToString();
                    var RelatedBill = DtIn.Rows[0]["RelatedBill"].ToString();
                    var PartNumber = DtIn.Rows[0]["PartNumber"].ToString();
                    var rowno = Convert.ToInt32(DtIn.Rows[0]["RowNo"]);
                    var orderno = Convert.ToInt32(DtIn.Rows[0]["OrderNo"]);
                    var version = DtIn.Rows[0]["Version"].ToString();
                    var datecode = DtIn.Rows[0]["DateCode"].ToString();
                    var SupLotNo = DtIn.Rows[0]["SupLotNo"].ToString();
                    var PlanQty = Convert.ToDecimal(DtIn.Rows[0]["PlanQty"]);
                    var RealQty = Convert.ToDecimal(DtIn.Rows[0]["RealQty"]);
                    var Unit = DtIn.Rows[0]["Unit"].ToString();
                    var CustomerSN = DtIn.Rows[0]["CustomerSN"].ToString();
                    var ExpiryDate = Convert.ToDateTime(DtIn.Rows[0]["ExpiryDate"].ToString());
                    var Ext1 = DtIn.Rows[0]["Ext1"].ToString();
                    var oldLocation_SN = DtIn.Rows[0]["Location_SN"].ToString();
                    string Storage_SN = DtIn.Rows[0]["Storage_SN"].ToString(); //仓库
                    string Area_SN = string.Empty;    //库区
                    string Location_SN = txtSN.Text;//库位

                    try
                    {
                        var snArray = Location_SN.Split('#');
                        if (!string.IsNullOrEmpty(Storage_SN) && !Storage_SN.Equals(snArray[0]))
                        {
                            MessageBox.Show("出库仓库" + Storage_SN + "退捡仓库" + snArray[0] + "不一致！");
                            return;
                        }
                        Storage_SN = snArray[0];
                        Area_SN = snArray[0] + '#' + snArray[1];
                        txtSN.Text = snArray[1] + snArray[2];
                    }
                    catch
                    {
                        MessageBox.Show("库位" + txtSN.Text + "格式不正确，请重新输入!");
                        txtSN.Text = "";
                        return;
                    }

                    var InveDt = Bll_RMIn.GetInventory(Location_SN, PartNumber);

                    bool inveBool = false;
                    bool inveNEW = true;
                    if (InveDt != null && InveDt.Rows.Count > 0)
                    {
                        //if ((datecode != InveDt.Rows[0]["DateCode"].ToString()) || (version != InveDt.Rows[0]["version"].ToString()))
                        //{
                        //    inveBool = true;
                        //}
                        //else
                        //{
                            inveNEW = false;
                        //}
                    }
                    //if (inveBool)
                    //{
                    //    MessageBox.Show("该库位物DateCode或版本与入库产品不符!");
                    //    return;
                    //}
                    //Bll_RMOut.UpdateRMDetailByUniqueCode(txtBoxCode.Text, txtBillNos);
                    bool inStock = Bll_RMOut.RMPR(billno, RelatedBill, PartNumber, rowno, orderno, version, datecode, SupLotNo, PlanQty, RealQty, Unit, Storage_SN, Area_SN, Location_SN, CustomerSN, ExpiryDate, txtBoxCode.Text, Ext1, oldLocation_SN, boolToERP, inveNEW);

                    if (inStock)
                    {
                        lab_msg.Text = txtBoxCode.Text + "退捡成功！\r\n" + GetBillInfoMsg(billno, rowno.ToString(), orderno.ToString(), PartNumber);

                    }
                    else
                    {
                        lab_msg.Text = txtBoxCode.Text + "退捡失败!";

                    }
                    txtBillNo.Focus();
                    txtBillNo.Text = "";
                    lblSN.Text = "";
                    txtBoxCode.Text = "";
                    txtSN.Text = "";
                }
                catch 
                {
                    lab_msg.Text = "数据有错误!";
                }
            }
        }


        public string GetBillInfoMsg(string billNo, string rowno, string orderno, string PartNumber)
        {
            try
            {
                var dt = Bll_RMOut.getRMDetails(billNo,Convert.ToInt32(rowno));
                var dtAllRows = Bll_RMIn.GetRMRowsAll(billNo, rowno);
                StringBuilder sb = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    var rmDetailCount = dt.Rows.Count;
                    int rmDetailOverCount = 0;
                    decimal planQtyCount = 0;
                    decimal realQtyCount = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        planQtyCount = planQtyCount + Convert.ToDecimal(dr["PlanQty"]);
                        if (dr["Status"].ToString() == "9")
                        {
                            rmDetailOverCount = rmDetailOverCount + 1;
                            realQtyCount = realQtyCount + Convert.ToDecimal(dr["RealQty"]);
                        }
                    }

                    sb.AppendLine("料号:" + PartNumber + "\r\n上架托数:" + rmDetailOverCount + "/" + rmDetailCount + "\r\n上架数量:" + realQtyCount + "/" + planQtyCount);

                }
                else
                {
                    string rownum = "";
                    if (dtAllRows != null && dtAllRows.Rows.Count > 0)
                    {
                        rownum = dtAllRows.Rows[0]["PlanQty"].ToString();
                    }
                    sb.AppendLine("料号:" + PartNumber + "\r\n上架托数:0\r\n上架数量:0/" + rownum);
                }

                return sb.ToString();
            }
            catch 
            {
                return "";
            }
        }

        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    var txtbillnoList = txtBillNo.Text.Split('-');
                    if (txtbillnoList.Length >= 2)
                    {
                        txtBillNos = txtbillnoList[0];
                        txtRowNo = txtbillnoList[1];
                        txtBoxCode.Focus();
                    }
                    else
                    {
                        MessageBox.Show("请选择正确的单号!");
                        txtBillNo.Text = "";
                        return;
                    }
                    var DtRMDetail = Bll_RMOut.getRMDetails(txtBillNos, Convert.ToInt32(txtRowNo));
                    if (!(DtRMDetail != null && DtRMDetail.Rows.Count > 0))
                    {
                        MessageBox.Show("该订单没有出库纪录，请选择正确的单号!");
                        txtBillNo.Text = "";
                        return;
                    }
                } 
                catch 
                {
                    MessageBox.Show("数据有错误!"); 
                    return;
                }

            }
        }

     
      

       
    }
}